package com.efounder.activity;

import android.os.Bundle;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.mobilemall.R;
import com.efounder.utils.GlideImageLoader;
import com.efounder.view.PhotoViewPager;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;

/**
 * Created by wei on 2018/2/05/05.
 * 商品图片缩放
 */

public class ImageActivity extends AppCompatActivity {

    PhotoViewPager viewPager;
    LinearLayout mLinearLayout;

    private int index;//点击的图片index
    private GlideImageLoader imageLoader;
    private ArrayList<String> list;
    int currPosition = 0; // 当前滑动到了哪一页
    private int mNum = 0;//默认index

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        viewPager = findViewById(R.id.imageViewPager);
        mLinearLayout = findViewById(R.id.main_linear);
        imageLoader = new GlideImageLoader();
        list = getIntent().getStringArrayListExtra("pic");
        index = getIntent().getIntExtra("position", 0);
        mNum = index;
        initAdapter();
    }


    private void initAdapter() {
        //初始化PagerAdapter
        PagerAdapter adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object o) {
            }

            @Override
            public Object instantiateItem(ViewGroup container, final int position) {
//                SketchImageView im = new SketchImageView(ImageActivity.this);
//                im.displayImage(list.get(position));
//                im.setZoomEnabled(true);
                final PhotoView im = new PhotoView(ImageActivity.this);
                LXGlideImageLoader.getInstance().displayImage(ImageActivity.this, im, list.get(position));
                im.canZoom();
                container.addView(im);
                im.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
                    @Override
                    public boolean onSingleTapConfirmed(MotionEvent e) {
                        finish();
                        return true;
                    }

                    @Override
                    public boolean onDoubleTap(MotionEvent e) {
                        if (im.getScale() > 1) {
                            im.setScale(1f, true);
                        } else {
                            im.setScale(im.getMediumScale(), true);
                        }
                        return true;
                    }

                    @Override
                    public boolean onDoubleTapEvent(MotionEvent e) {
                        return false;
                    }
                });
                return im;
            }
        };
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(index);
        for (int i = 0; i < list.size(); i++) {
            //创建底部指示器(小圆点)
            View view = new View(this);
            view.setBackgroundResource(R.drawable.circlebg);
            view.setEnabled(false);
            //设置宽高
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 20);
            //设置间隔
            if (i != 0) {
                layoutParams.leftMargin = 30;
            }
            //添加到LinearLayout
            mLinearLayout.addView(view, layoutParams);

        }
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currPosition = position;
                if (position < list.size()) {
                    //设置宽度并设置是否是当前选中状态
                    setIndicator(20, mNum, false);
                    setIndicator(60, position, true);
                    mNum = position;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //默认显示第index个指示器
        setIndicator(60, index, true);
    }

    private void setIndicator(int width, int index, boolean able) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, 20);
        layoutParams.leftMargin = 30;
        View childAt = mLinearLayout.getChildAt(index);
        childAt.setLayoutParams(layoutParams);
        childAt.setEnabled(able);
    }
}
